! Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
! See https://llvm.org/LICENSE.txt for license information.
! SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
!
! test for intrinsic sin take quad complex argument.

program test
  implicit none
  integer, parameter :: n = 16
  integer, parameter :: m = n * 2
  real(16), parameter :: pi_q = 3.1415926535897932384626433832795028841_16
  ! Changed deviation because of different glibc versions (2.17<-->2.28)
  ! real(16), parameter :: q_tol = 5e-33_16
  real(16), parameter :: q_tol = 3e-29_16
  complex(16), dimension(n), parameter :: radians = (/ &
    (0.0_16, 0.0_16),                                                                         & ! pi * 0
    (0.523598775598298873077107230546583832_16, -0.523598775598298873077107230546583832_16),  & ! pi * 1/6
    (0.785398163397448309615660845819875699_16, -0.785398163397448309615660845819875699_16),  & ! pi * 1/4
    (1.04719755119659774615421446109316766_16, -1.04719755119659774615421446109316766_16),    & ! pi * 1/3
    (1.57079632679489661923132169163975140_16, -1.57079632679489661923132169163975140_16),    & ! pi * 1/2
    (2.09439510239319549230842892218633533_16, -2.09439510239319549230842892218633533_16),    & ! pi * 2/3
    (2.35619449019234492884698253745962710_16, -2.35619449019234492884698253745962710_16),    & ! pi * 3/4
    (2.61799387799149436538553615273291925_16, -2.61799387799149436538553615273291925_16),    & ! pi * 5/6
    (3.14159265358979323846264338327950280_16, -3.14159265358979323846264338327950280_16),    & ! pi
    (3.66519142918809211153975061382608673_16, -3.66519142918809211153975061382608673_16),    & ! pi * 7/6
    (3.92699081698724154807830422909937850_16, -3.92699081698724154807830422909937850_16),    & ! pi * 5/4
    (4.18879020478639098461685784437267065_16, -4.18879020478639098461685784437267065_16),    & ! pi * 4/3
    (4.71238898038468985769396507491925420_16, -4.71238898038468985769396507491925420_16),    & ! pi * 3/2
    (5.23598775598298873077107230546583851_16, -5.23598775598298873077107230546583851_16),    & ! pi * 5/3
    (5.49778714378213816730962592073912990_16, -5.49778714378213816730962592073912990_16),    & ! pi * 7/4
    (5.75958653158128760384817953601242205_16, -5.75958653158128760384817953601242205_16)     & ! pi * 11/6
  /)
  integer :: i
  complex(16), dimension(n) :: arg
  complex(16) :: result(m), expect(m)

  expect = (/ &
    (0.00000000000000000000000000000000000_16, 0.00000000000000000000000000000000000_16), &
    (0.570119160538214396070565990189675481_16, -0.474455025938597089696763154668468926_16), &
    (0.936640069431430098425763045692255005_16, -0.614243127486595649173396111132125673_16), &
    (1.38588907211263954816955651164173540_16, -0.624683525261987632477097650963987709_16), &
    (2.50917847865805678200999564326940585_16, -9.978213566735568596542884060072730046E-0035_16), &
    (3.56961473288598474636807876283839556_16, 1.99934567139991079555136599240671247_16), &
    (3.76375413950083480102026114473295659_16, 3.69673439979256143365321340800194938_16), &
    (3.44528618248794128768447122966214695_16, 5.90423497480638854944931690562387449_16), &
    (1.005232178425890674685851076404076182E-0033_16, 11.5487393572577483779773343153884089_16), &
    (-9.77230315838912677967460084937873105_16, 16.9039559588395256810159836355725423_16), &
    (-17.9512217021599064677145795253253078_16, 17.9372896670622122646038312524453406_16), &
    (-28.5607080050852010899940968507430460_16, 16.4819501450499670226377614509241273_16), &
    (-55.6633808904386777273653364424483761_16, 7.239365533328486037187131791729351012E-0033_16), &
    (-81.3717253183424980490066364025117668_16, -46.9773267342299272141585818599535052_16), &
    (-86.3218841818573384184872774546986069_16, -86.3189879963035258017461468327134589_16), &
    (-79.3050737358620385448439779237307073_16, -137.357686937249874224029535698032481_16), &
    (0.00000000000000000000000000000000000_16, 0.00000000000000000000000000000000000_16), &
    (0.570119160538214396070565990189675481_16, -0.474455025938597089696763154668468926_16), &
    (0.936640069431430098425763045692255005_16, -0.614243127486595649173396111132125673_16), &
    (1.38588907211263954816955651164173540_16, -0.624683525261987632477097650963987709_16), &
    (2.50917847865805678200999564326940585_16,-9.978213566735568596542884060072730046E-0035_16), &
    (3.56961473288598474636807876283839556_16, 1.99934567139991079555136599240671247_16), &
    (3.76375413950083480102026114473295659_16, 3.69673439979256143365321340800194938_16), &
    (3.44528618248794128768447122966214695_16, 5.90423497480638854944931690562387449_16), &
    (1.005232178425890674685851076404076182E-0033_16, 11.5487393572577483779773343153884089_16), &
    (-9.77230315838912677967460084937873105_16, 16.9039559588395256810159836355725423_16), &
    (-17.9512217021599064677145795253253078_16, 17.9372896670622122646038312524453406_16), &
    (-28.5607080050852010899940968507430460_16, 16.4819501450499670226377614509241273_16), &
    (-55.6633808904386777273653364424483761_16, 7.239365533328486037187131791729351012E-0033_16), &
    (-81.3717253183424980490066364025117668_16, -46.9773267342299272141585818599535052_16), &
    (-86.3218841818573384184872774546986069_16, -86.3189879963035258017461468327134589_16), &
    (-79.3050737358620385448439779237307073_16, -137.357686937249874224029535698032481_16) &
  /)


  do i = 1, n
    result(i) = sin(radians(i))
    arg(i) = radians(i)
  enddo

  do i = 1, n
    result(i+n) = sin(arg(i))
  enddo

  do i = 1, m
    if (expect(i)%re .eq. 0.0_16) then
      if (result(i)%re .ne. expect(i)%re) STOP i
    else
      if (abs((result(i)%re - expect(i)%re) / expect(i)%re) .gt. q_tol) STOP i
    endif
    if (expect(i)%im .eq. 0.0_16) then
      if (result(i)%im .ne. expect(i)%im) STOP i
    else
      if (abs((result(i)%im - expect(i)%im) / expect(i)%im) .gt. q_tol) STOP i
    endif
  enddo

  print *, 'PASS'
end
